﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Omu.Awesome.Mvc.Helpers
{
    public static class LookupHelpers
    {
        public static MvcHtmlString LookupFor<T, TReturn>(
            this HtmlHelper<T> html,
            Expression<Func<T, TReturn>> prop,
            object value = null,
            string controller = null,
            string title = null,
            int? height = null,
            int? width = null,
            string chooseText = null,
            string cancelText = null,
            bool? clearButton = null,
            object htmlAttributes = null,
            bool? multiselect = null,
            bool? fullscreen = null,
            string prefix = null,
            bool? paging = null,
            IDictionary<string,string> data = null)
        {
            return Lookup(html, ExpressionHelper.GetExpressionText(prop), value, controller, title, height, width, chooseText, cancelText, clearButton, htmlAttributes, multiselect, fullscreen, prefix, paging, data);
        }

        public static MvcHtmlString Lookup(
            this HtmlHelper html,
            string prop,
            object value = null,
            string controller = null,
            string title = null,
            int? height = null,
            int? width = null,
            string chooseText = null,
            string cancelText = null,
            bool? clearButton = null,
            object htmlAttributes = null,
            bool? multiselect = null,
            bool? fullscreen = null,
            string prefix = null,
            bool? paging = null,
            IDictionary<string, string> data = null)
        {
            if (html.ViewData.ModelMetadata != null)
            {
                var vals = html.ViewData.ModelMetadata.AdditionalValues;
                title = (string)title.FromMeta("title", vals);
                controller = (string)controller.FromMeta("controller", vals);
                clearButton = (bool?)clearButton.FromMeta("clearButton", vals);
                fullscreen = (bool?)fullscreen.FromMeta("fullscreen", vals);
                multiselect = (bool?)multiselect.FromMeta("multiselect", vals);
                width = (int?)width.FromMeta("width", vals);
                height = (int?)height.FromMeta("height", vals);
                paging = (bool?)paging.FromMeta("paging", vals);
            }
            
            value = value.FromModel(html, prop);
            
            if(Settings.GetText != null)
            {
                if(cancelText == null) cancelText = Settings.GetText("Lookup", "Cancel");
                if(chooseText == null) chooseText = Settings.GetText("Lookup", "Choose");
                if(title == null) title = Settings.GetText("Lookup", "Title");
            }

            var info = new LookupInfo
                           {
                               Prop = prop,
                               Title = title ?? Settings.Lookup.Title,
                               Height = height ?? Settings.Lookup.Height,
                               Width = width ?? Settings.Lookup.Width,
                               CancelText = cancelText ?? Settings.Lookup.CancelText,
                               ChooseText = chooseText ?? Settings.Lookup.ChooseText,
                               Value = value,
                               Controller = controller ?? prop.GetController() + "Lookup",
                               ClearButton = clearButton ?? Settings.Lookup.ClearButton,
                               Multiselect = multiselect ?? Settings.Lookup.Multiselect,
                               Fullscreen = fullscreen ?? Settings.Lookup.Fullscreen,
                               Prefix = prefix,
                               Paging = paging ?? Settings.Lookup.Paging,
                               Data = data
                           };

            var hd = htmlAttributes.GetDictionary();

            if (!html.ViewData.ModelState.IsValidField(prop)) hd = hd.AddVal("class", "input-validation-error");
            if (info.Multiselect) hd = hd.AddVal("class", "ae-lookup-multidisplay");
            info.HtmlAttributes = hd.ToHtml();

            return html.Partial(Settings.AwesomeFolder + (info.Multiselect ? "MultiLookup.ascx" : "Lookup.ascx"), info);
        }
    }
}
